<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <search :config="searchConfig" @search="search" @getHeight="getHeight"/>
    <div class="page_table">
      <vxe-grid
        ref="xGrid"
        :border="true"
        :resizable="true"
        :showOverflow="true"
        size="mini"
        :loading="loading"
        :height="pageHeight-seachHeight-94"
        :align="left"
        :columns="columns"
        :data="dataSource"
        :toolbar-config="tableToolbar"
        :checkbox-config="checkboxConfig"
        v-bind="vexConfig"
        @checkbox-change="selectChangeEvent"
        @checkbox-all="selectChangeEvent"
      >
        <template #toolbar_buttons>
          <div class="table-operator">
            <a-button v-has="'finalGroup:add'" @click="handleAdd">新增</a-button>
            <a-button v-has="'finalGroup:delete'" @click="batchDel">删除</a-button>
            <a-button v-has="'finalGroup:synchronization'" @click="downExportXls('cnProduct_service')">同步</a-button>
            <a-button v-has="'finalGroup:export'" @click="downExportXls('cnProduct_service' )">导出</a-button>
          </div>
        </template>

        <template #finalCustomerCode="{ row,rowIndex }">
          <a @click="handleSelect(row)">{{ row.finalCustomerCode }}</a>
        </template>

        <template #action="{ row,rowIndex }">
          <a v-has="'finalGroup:update'" @click="handleEdit(row)">编辑</a>
          <a-divider type="vertical"/>
          <a href='javascript:' @click='handleBusiness("团购经销商",row)'>日志</a>
          <!--          <a-dropdown>-->
          <!--            <a class='ant-dropdown-link'>-->
          <!--              更多-->
          <!--&lt;!&ndash;              <a-icon type='down' />&ndash;&gt;-->
          <!--            </a>-->
          <!--            <a-menu slot='overlay'>-->
          <!--              <a-menu-item>-->
          <!--                <a-popconfirm title='确定删除吗?' @confirm='() => handelDeleteAction(row.id)'>-->
          <!--                  <a>删除</a>-->
          <!--                </a-popconfirm>-->
          <!--              </a-menu-item>-->
          <!--              <a-menu-item v-has="'business:log'">-->
          <!--                <a href='javascript:;' @click='handleBusiness("团购经销商",row)'>日志</a>-->
          <!--              </a-menu-item>-->
          <!--            </a-menu>-->
          <!--          </a-dropdown>-->
        </template>
        <template #update="{ row }">
          <a-switch :checked="row.status" size="small" @change="handleStatus(row,$event)"/>
        </template>
        <template #pager>
          <vxe-pager
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :currentPage.sync="ipagination.current"
            :page-size.sync="ipagination.pageSize"
            :total="ipagination.total"
            :pageSizes="ipagination.pageSizeOptions"
            @page-change="handlePageChange">
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <!-- 操作按钮区域 -->
    <business-log ref="businessLog"></business-log>
    <finalGroupModel ref="modalForm" @ok="modalFormOk"></finalGroupModel>
  </a-card>
</template>

<script>
import finalGroupModel from './modules/FinalGroupModel.vue'
import { PageListMixin } from '@/mixins/PageListMixin'
import { getAction, postAction } from '@api/manage'
import Vue from 'vue'

export default {
  name: 'userorgerl',
  mixins: [PageListMixin],
  components: {
    finalGroupModel
  },
  data() {
    return {
      selectedRowKeys: [],
      description: '最终团购经销商',
      searchConfig: [
        {
          type: 'input',
          name: 'customerCode',

          placeholder: '经销商编号'
        },
        {
          type: 'input',
          name: 'customerName',
          placeholder: '经销商名称'
        },
        {
          type: 'input',
          name: 'contacts',
          placeholder: '联系人'
        },
        {
          type: 'a-select',
          name: 'status',
          placeholder: '请选择状态',
          option: [
            {
              name: '启用',
              value: 1
            },
            {
              name: '停用',
              value: 0
            }]
        },
      ],
      //表头
      columns: [],
      //列设置
      settingColumns: [],
      defColumns: [
        { type: 'checkbox', width: 60, fixed: 'left' },
        {
          fixed: 'left',
          title: '最终经销商编号',
          field: 'finalCustomerCode',
          minWidth: 100,
          slots: { default: 'finalCustomerCode' }

        },
        {
          title: '经销商编号',
          field: 'customerCode',
          minWidth: 80,
        },
        {
          title: '经销商名称',
          field: 'customerName',
          minWidth: 80,
        },
        {
          title: '联系人',
          minWidth: 80,
          field: 'contacts',

        },
        {
          title: '联系方式',
          minWidth: 80,
          field: 'contactPhone',
        },
        {
          title: '收货地址',
          minWidth: 80,
          field: 'receivedGoodsAddress',
        },
        {
          title: '回款账号',
          minWidth: 80,
          field: 'receivableAccount',
        },
        {
          title: '开户银行',
          minWidth: 80,
          field: 'customerBank',
        },
        {
          title: '返佣对象',
          minWidth: 80,
          field: 'rebateObj',
        },
        {
          title: '备注',
          minWidth: 80,
          field: 'remarks',
        },
        {
          title: '创建人',
          minWidth: 80,
          field: 'createName',
        },
        {
          title: '创建时间',
          minWidth: 80,
          field: 'createTime',
        },
        {
          title: '更新人',
          minWidth: 80,
          field: 'updateName',
        },
        {
          title: '更新时间',
          minWidth: 80,
          field: 'updateTime',
        },
        {
          title: '状态',
          field: 'status',
          width: 60,
          fixed: 'right',
          minWidth: 80,
          slots: { default: 'update' }
        },
        {
          title: '操作',
          width: 100,
          fixed: 'action',
          fixed: 'right',
          slots: { default: 'action', filterDropdown: 'filterDropdown' }
        }
      ],
      isorter: {
        permflag: 'purchase_customer_page_impl'
      },
      url: {
        list: window._CONFIG['domianURL'] + '/customer/cnPurchaseCustomer/findPageList',
        edit: window._CONFIG['domianURL'] + '/mdsProduct/saveOrUpdateEntity',
        deleteBatch: '/customer/cnPurchaseCustomer/deleteBatch',
        delete: window._CONFIG['domianURL'] + '/userorgrelation/cnUserOrgRelation/delete',
        startOrStopUrl: window._CONFIG['domianURL'] + '/customer/cnPurchaseCustomer/updateStatus',
        exportXlsUrl: window._CONFIG['domianTpmURL'] + 'budget/tpmBudgetFee/exportXls'
      }
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.initColumns()
  },
  methods: {
    //列表设置更改事件
    onColSettingsChange(checkedValues) {
      var key = this.$route.name + ':colsettings'
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
      this.settingColumns = checkedValues
      const cols = this.defColumns.filter(item => {
        if (item.key == 'rowIndex' || item.dataIndex == 'action') {
          return true
        }
        if (this.settingColumns.includes(item.dataIndex)) {
          return true
        }
        return false
      })
      this.columns = cols
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
    },
    changeStatus(e, item) {

      if (e) {
        this.startorstopByid(1, item.id)
      } else {
        this.startorstopByid(0, item.id)
      }
    },
    startorstopByid(status, id) {
      let that = this
      this.$confirm({
        title: '确认修改',
        content: '是否' + (status == 0 ? '停用' : '启动') + '选中数据？',
        onOk() {
          that.loading = true
          getAction(that.url.startOrStopUrl, { ids: id, status: status }).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.loadData()
              that.onClearSelected()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.loading = false
          })
        },
        onCancel() {
          // that.loadData();
          // that.onClearSelected();
        }
      })
    },
    handleSelect: async function (row) {
      this.$refs.modalForm.edit(row)
      this.$refs.modalForm.title = '详情'
      this.$refs.modalForm.disableSubmit = true
    },
    handleEdit: async function (row) {
      this.$refs.modalForm.title = '编辑'
      this.$refs.modalForm.edit(row)
      this.$refs.modalForm.disableSubmit = false
    },
    initColumns() {
      //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
      //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');

      var key = this.$route.name + ':colsettings'
      let colSettings = Vue.ls.get(key)
      if (colSettings == null || colSettings == undefined) {
        let allSettingColumns = []
        this.defColumns.forEach(function (item, i, array) {
          allSettingColumns.push(item.dataIndex)
        })
        this.settingColumns = allSettingColumns
        this.columns = this.defColumns
      } else {
        this.settingColumns = colSettings
        const cols = this.defColumns.filter(item => {
          if (item.key == 'rowIndex' || item.dataIndex == 'action') {
            return true
          }
          if (colSettings.includes(item.dataIndex)) {
            return true
          }
          return false
        })
        this.columns = cols
      }
    },
  }
}
</script>

<style scoped>
@import '~@assets/less/common.less';
</style>